<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="chrome=1"/>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>G-Code Coder</title>
    <script src="../libs/require.js"></script>
    <script src="../config.js"></script>
    <script>
        requirejs.config({ baseUrl: '../' });
    </script>

    <link rel="shortcut icon" href="../images/icon_fraise_48.png"/>
    <link rel="stylesheet" href="../css/twoDView.css" type="text/css">
    <link rel="stylesheet" href="../css/threeDView.css" type="text/css">
	<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
    <link href="https://cdn.jsdelivr.net/npm/quasar@2.15.1/dist/quasar.prod.css" rel="stylesheet" type="text/css">
    <style>

        body, html {
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            position: absolute;
            height: 100%;
            width: 100%;
            margin: 0;
        }

        body {
            padding:0;
        }

        h1 {
            margin-top: 0;
        }

        .editBlock {
            position: relative;
            float: left;
            width: 39%;
            height: 400px;
            padding: 1px;
            margin: 0;
        }

        .editBlock pre {
            width: 100%;
            height: 350px;
            margin: 0;
        }

        .viewContainer {
            float: right;
            width: 60%;
        }

        #loader {
            display: inline-block;
            background-size: 100% 100%;
            background-image: url(../images/spinner.svg);
            width: 20px;
            height: 20px;
        }

        .boundsTable {
            border-collapse: collapse;
        }

        .boundsTable td {
            border: dashed gray 1px;
            padding: 3px;
        }

        .gcode-visualizer {
            border: solid gray 1px;
            background: #000;
            position: relative;
        }

        .TwoDView {
            border: solid gray 1px;
            background: #000;
            height: 400px;
        }

        #app {
            position: relative;
        }
    </style>
</head>
<body>

<div id="app" style="height:100%"></div>

<script id="demoJS" type="text/plain">const width = 600;
const depth = 300;

const part = {height:80, width:50, corner:15};

cnc.size(width, depth);
cnc.push("(begin preamble)");
cnc.push("G17 G54 G40 G49 G80 G90");
cnc.push("G21 G64 P0.015");
cnc.push("M6 T1");
cnc.push("G0 Z15");
cnc.push("M3 S10000");
cnc.push("");
cnc.push("(begin cut)");
cnc.push("G0 X0 Y"+depth);

var forward = true;
var rows = Math.floor(depth / (part.width + (part.width - 2*part.corner))) * 2;
var cols = Math.floor(width / part.height) + 1;
var baseY = depth - part.corner;

// Описываемый профиль
cnc.path('joint', [
    {x:0, y:part.width},
    {x:0, y:0},
    {x:part.height/2, y:part.corner},
    {x:part.height, y:0},
    {x:part.height, y:part.width}
])

for(var r=0; r < rows; r++) {
    cnc.push("(row "+r+")");

    if(r > 0) baseY -= (part.width - part.corner);
    var offsetX = r % 2 > 0 ? 0 : -part.height/2;

    for(var c=0; c <= cols; c++) {
        var xPos = forward ? c*part.height + offsetX : cols*part.height - (c*part.height+offsetX);

        // TODO: Нужно выставить правильную последовательность реза, иначе детали будут выпадать недорезанными

        cnc.push('G0 X{x} Y{y}', cnc.path('joint').steps(0), {offset:{x:xPos, y:baseY}} );
        cnc.push('G1 F{feed} Z{z}', {feed:cnc.feed(300), z:-6.0});
        cnc.path('joint').forward( function(step){ cnc.push('G1 F{feed} X{x} Y{y}', step, {offset:{x:xPos, y:baseY}}) } );
        cnc.push('G1 F{feed} Z{z}', {feed:cnc.feed(300), z:-12.5});
        cnc.path('joint').backward( function(step){ cnc.push('G1 F{feed} X{x} Y{y}', step, {offset:{x:xPos, y:baseY}}) } );
        cnc.push('G0 F{feed} Z{z}', {feed:cnc.feed(300), z:15.0});
        cnc.push('');
    }

    forward = !forward;
}

cnc.push("G0 Z15.0");
cnc.push("M5");
cnc.push("M2");
</script>

<script id="demoCode" type="application/gcode">G0 Y10 Z-5
G1 Z-10
G1 Y20
G02 X10 Y30 R10
G1 X30
G2 X40 Y20 R10
G1 Y10
G2 X30 Y0 R10
G1 X10
G2 X0 Y10 Z-15 R10 (yeah spiral !)
G3 X-10 Y20 R-10 (yeah, long arc !)
G3 X0 Y10 I10 (center)
G91 G1 X10 Z10
G3 Y10 R5 Z3 (circle in incremental)
Y10 R5 Z3 (again, testing modal state)
G20 G0 X1 (one inch to the right)
G3 X-1 R1 (radius in inches)
G3 X1 Z0.3 I0.5 J0.5 (I,J in inches)
G21 (back to mm)
G80 X10 (do nothing)
G90
G0 X30 Y30 Z30
G18 (X-Z plane)
G3 Z40 I0 K5
G19 (Y-Z plane)
G3 Z50 J0 K5
G17 (back to X-Y plane)
</script>

<template id="tpl-app">
	<div style="height:100%;display:flex;flex-direction:row">
		<div style="width:40%;height:100%;display:flex;flex-direction:column">
			<q-tabs v-model="ui.tab" ref="codeTabs">
				<q-tab name="javascript">JavaScript</q-tab>
				<q-tab name="gcode">G-Code</q-tab>
			</q-tabs>
			<q-tab-panels keep-alive v-model="ui.tab" ref="codePanels" @before-transition="onBeforeTab" @transition="onAfterTab" style="height:100%">
				<q-tab-panel class="q-pa-none" name="javascript"><js-editor source="#demoJS" ref="jsCode"></js-editor></q-tab-panel>
				<q-tab-panel class="q-pa-none" name="gcode"><gcode-editor ref="gCode"></gcode-editor></q-tab-panel>
			</q-tab-panels>
		</div>
		<div style="width:60%">
			<gcode-visualizer-3d ref="gCodeView"></gcode-visualizer-3d>
		</div>
	</div>
</template>

<template id="tpl-editor">
	<div class="ace-editor" style="min-width:400px;height:100%"></div>
</template>

<script src="js/gcodeCoder.js" type="text/javascript"></script>
</body>
</html>
